package com.ea.controller.back;

import com.ea.api.controller.admin.InventoryInfoApi;
import com.ea.common.enums.OperationResultCode;
import com.ea.common.exception.ServiceException;
import com.ea.common.response.JsonResult;
import com.ea.domain.dto.ElectricGoodStockChangeDTO;
import com.ea.domain.dto.InventoryDTO;
import com.ea.domain.query.InventoryQueryDTO;
import com.ea.domain.vo.InventoryVO;
import com.ea.service.IInventoryInfoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 库存信息表 前端控制器
 * </p>
 *
 * @author 电器商城
 * @since 2022-11-29
 */
@RestController
@RequestMapping("/electric/back/inventory")
public class InventoryInfoController implements InventoryInfoApi {


    @Autowired
    private IInventoryInfoService inventoryInfoService;

    @Override
    @PostMapping("/info/list")
    public JsonResult getGoodsInventoryList(@RequestBody InventoryQueryDTO queryDTO) {
        PageInfo<InventoryVO> inventoryList = inventoryInfoService.getInventoryList(queryDTO);
        return JsonResult.querySuccessFul(inventoryList);
    }

    @Override
    @PostMapping("/stock/record")
    public JsonResult stockRecordInventory(@RequestBody ElectricGoodStockChangeDTO changeDTO) {
        if (changeDTO == null || changeDTO.getSkuId() == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        inventoryInfoService.changeInventoryInfo(changeDTO.getSkuId(), changeDTO.getAmount());
        return JsonResult.updateSuccessFul();
    }

    @GetMapping("/stock/surplus")
    public JsonResult getElectricSurplus( ) {
        List<InventoryDTO> electricSurplus = inventoryInfoService.getElectricSurplus();
        return JsonResult.doSuccessful(electricSurplus);
    }

}
